using Godot;
using System;

using CurrentGame.NPC;
using CurrentGame.GUI.PlaceGrid;

namespace CurrentGame.Levels.Level3;


public partial class NormalLevel1Main : Node
{
    // Actually Level3

    [Export]
    public Godot.Collections.Array<ShadeCharacter> ReadyShades { get; set; } = [];

    [Export]
    public DialogueConfig Dialogue1 { get; set; }

    [Export]
    public LevelTeleport Teleport { get; set; }

    [Export]
    public Area2D EnterLevelArea { get; set; }


    private SceneSwitch _sceneSwitch;
    private PlaceModeGrid _placeGrid;
    private Player _player;
    private bool _hardMode = false;
    

    public override void _Ready()
    {
        var map = GetNode<NodeReferenceMap>(Constants.NodeRefMapPath);
        _sceneSwitch = map.SceneSwitch;
        _sceneSwitch.SceneStart += Signal_SceneStart;
        _placeGrid = map.PlacementModeGrid;
        _player = map.MainPlayer;
        _hardMode = map.ProgressManager.HardMode;

        _player.AttackEnabled = false;
        _player.PlaceModeAvailable = false;

        _player.SetHealthPoint(3);

        _placeGrid.FreePlaceCost = !_hardMode;
        Teleport.NextSceneName = _hardMode ? "hard_end" : "end_level";
    }

    public override void _ExitTree()
    {
        _sceneSwitch.SceneStart -= Signal_SceneStart;
        _placeGrid.ClearAllShade();
    }

    private void Signal_EnterLevel(Area2D area)
    {
        _player.AttackEnabled = true;
        _player.PlaceModeAvailable = true;
        Dialogue1.QueueFree();
        EnterLevelArea.QueueFree();
    }

    private void Signal_SceneStart()
    {
        GD.Print("Started Level 3");
        foreach (var shade in ReadyShades)
        {
            shade.ActivateFallDetection();
            if (!_hardMode)
                _placeGrid.AppendReadyShade(shade);
            else
                _placeGrid.AppendNotReadyShade(shade);
        }
    }

}
